/*
 * Created on Oct 18, 2003
 *
 * To change the template for this generated file go to
 * Window>Preferences>Java>Code Generation>Code and Comments
 */
package schoolsource.model;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.logging.Level;

import schoolsource.sql.SchoolDBSQL;
import schoolsource.util.SchoolLogger;

/**
 * 
 * @author Steven Steele
 * Copyright 2003
 *
 * Contains information for messages.
 */
public class MessageContainer extends Container {

	// The map of timer buckets for all databases
	protected static Hashtable dbStores = new Hashtable(); 
	// The auto-generated id
	private IntegerItem assignedMessageID =
		new IntegerItem(SchoolDBSQL.MSGASSIGNEDID, "", true);
	// The date on which the message was sent
	private DateItem dateTimeSent =
		new DateItem(SchoolDBSQL.MSGTIMESENT, "", true);
	// The subject of the message
	private StringItem subject =
		new StringItem(SchoolDBSQL.MSGSUBJECT, "", true);
	// The body of the message
	private StringItem messageBody =
		new StringItem(SchoolDBSQL.MSGBODY, "", true);

	/**
	 * Default constructor.
	 */
	private MessageContainer() {

	}

	/**
	 * Returns a new instance of the container with values loaded from the database
	 * corresponding to the passed-in message ID.
	 * @param newMsgAssignedID The auto-generated message ID of the container to be loaded.
	 */
	private MessageContainer(String newMsgAssignedID, String dbName) {
		getAssignedMessageID().setValue(newMsgAssignedID);
		SchoolDBSQL.getSchoolDBSQL(dbName).loadMessage(
			this,
			new Item[] { getAssignedMessageID()});
	}

	public static MessageContainer getMessageContainer(String mID, String dbName) {
		MessageContainer returnVal = null;
		DBStore dbs = null;
		
		if (mID == "-1") {
			returnVal = new MessageContainer();
		}
		else {
			if ((dbs = (DBStore) dbStores.get(dbName)) == null) {
				dbs = new DBStore();
				dbStores.put(dbName, dbs);
				dbs.start();
			}
			if ((returnVal = (MessageContainer) dbs.getContainer(mID)) == null) {
				returnVal = new MessageContainer();
				returnVal.getAssignedMessageID().setValue(mID);
				SchoolDBSQL.getSchoolDBSQL(dbName).loadMessage(returnVal, new Item[] {returnVal.getAssignedMessageID()});
				dbs.addContainer(returnVal, mID);
			}
		}

		return returnVal;
	}

	/* (non-Javadoc)
	 * @see schoolsource.model.Container#getValidationItems()
	 */
	public Item[] getValidationItems() {
		ArrayList al = new ArrayList();
		al.add(assignedMessageID);
		al.add(dateTimeSent);
		al.add(subject);
		al.add(messageBody);

		return (Item[]) al.toArray(new Item[al.size()]);
	}

	/**
	 * Getter for the assigned message ID.
	 * @return The auto-generated ID of the message.
	 */
	public IntegerItem getAssignedMessageID() {
		return assignedMessageID;
	}

	/**
	 * Getter for the date and time the message was sent.
	 * @return The date and time the message was sent.
	 */
	public DateItem getDateTimeSent() {
		return dateTimeSent;
	}

	/**
	 * Getter for the body of the message.
	 * @return The body of the message.
	 */
	public StringItem getMessageBody() {
		return messageBody;
	}

	/**
	 * Getter for the subject of the message.
	 * @return The subject of the message.
	 */
	public StringItem getSubject() {
		return subject;
	}

	/**
	 * Prints the values of the container.
	 */
	public void printItems() {
		SchoolLogger.logger.log(
			Level.INFO,
			"************************************************");
		Item iList[] = getValidationItems();
		for (int j = 0; j < iList.length; j++) {
			if (iList[j] != null) {
				SchoolLogger.logger.log(
					Level.INFO,
					iList[j].getFieldName() + ": " + iList[j].toString());
			}
		}
	}
}
